﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Model;
using LinkCard.Dao;
using LinkCard.AdminCP.Common;
using System.Data;
namespace LinkCard.AdminCP.Message
{
    public partial class message_new : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
                {
                    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "HOMEPAGE") == false)
                    {
                        Response.Redirect("/message_box.aspx?why=1&msg=5");
                    }
                }
            }

            if (!IsPostBack)
            {
                DataTable dt;
                // Category Option 
                dt = DataHelper.SelectTable("SELECT CategoryID, Name FROM MessageCate ORDER BY Name", false);
                CategoryID.DataSource = dt;
                CategoryID.DataTextField = "Name";
                CategoryID.DataValueField = "CategoryID";
                CategoryID.DataBind();

                MsgPostDate.Value = DateTime.Now.ToString();
                MsgHomeImage.ToolbarSet = "Image";
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            add();
            Response.Redirect("/Message/message_all_published.aspx");
        }

        protected void add()
        {
            var dao = new LinkCard.Dao.MessageDao();
            var item = new LinkCard.Model.Message();

            string sMsgHomeImage = MsgHomeImage.Value.Replace("<p>","");
            sMsgHomeImage = sMsgHomeImage.Replace("</p>","");
            sMsgHomeImage = sMsgHomeImage.Replace("<P>", "");
            sMsgHomeImage = sMsgHomeImage.Replace("</P>", "");

            item.MsgTitle = MsgTitle.Value;
            item.MsgSubTitle = MsgSubTitle.Value;
            item.MsgLead = MsgLead.Value;
            item.MsgContent = MsgContent.Value;
            
            item.MsgHomeImage = sMsgHomeImage ;
            item.MsgImageComment = MsgImageComment.Value;
            item.MsgPostDate = ConvertUtility.ToDateTime(MsgPostDate.Value);
            item.CategoryID = ConvertUtility.ToInt32(CategoryID.SelectedValue);
            item.MsgStatus = 2;
            item.MsgHistory = "Bài viết được tạo bởi " + Session["USER"].ToString() + " lúc " + DateTime.Now.ToString() + "";
            //item.Tags = txtTags.Value;
            item.IsHome = IsHome.Checked;
            dao.Add(item);

            //if (!string.IsNullOrEmpty(item.Tags))
            //{
            //    var addTag = new TagDao().AddMessageTag(item.MessageID, item.Tags);
            //}
        }

        protected Int64 getUserID()
        {

            DataTable dt = DataHelper.SelectTable("SELECT UserID FROM [User] WHERE Username ='" + Session["USER"].ToString() + "'", false);
            if (dt.Rows.Count > 0)
            {
                return ConvertUtility.ToInt64(dt.Rows[0]["UserID"]);
            }
            else
            {
                return 0;
            }
        }
    }
}
